package com.lanou3g.baidumusic.main.playactivity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.lanou3g.baidumusic.R;
import com.lanou3g.baidumusic.main.mainactivity.MusicService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 17/3/17.
 */

public class PlayActivity extends AppCompatActivity {


    private ViewPager viewPager ;
    private PlayMusicAdapter adapter ;
    private List<Fragment> fragments ;
    private ImageView playIv,nextIm,lastIv,moshiIv,listIv;
    private TextView nowTv ;
    private SeekBar seekBar ;

    private MusicService.MusicBinder musicBinder ;
    private ServiceConnection connection ;
    private boolean isMoveBar = false ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        viewPager = (ViewPager) findViewById(R.id.activity_play_vp_vp);


        fragments = new ArrayList<>();
        fragments.add(new FirstFragment());
        fragments.add(new SecondFragment());
        fragments.add(new ThirdFragment());

        adapter = new PlayMusicAdapter(getSupportFragmentManager() , fragments);

        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(1);

        playIv = (ImageView) findViewById(R.id.activity_play_play_iv);
        nextIm = (ImageView) findViewById(R.id.activity_play_next_iv);
        lastIv = (ImageView) findViewById(R.id.activity_play_last_iv);
        moshiIv = (ImageView) findViewById(R.id.activity_play_moshi_iv);
        listIv = (ImageView) findViewById(R.id.activity_play_list_iv);
        nowTv = (TextView) findViewById(R.id.activity_play_now_tv);
        seekBar = (SeekBar) findViewById(R.id.main_activity_seek_bar_sb);

        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                musicBinder = (MusicService.MusicBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                musicBinder = null ;
            }
        };

        Intent serviceIntent = new Intent(this,MusicService.class);
        bindService(serviceIntent,connection,BIND_AUTO_CREATE);

        //点击播放
        playIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (musicBinder.isPlaying()){
                    musicBinder.pause();
                    playIv.setImageResource(R.mipmap.play);
                }else {
                    if (musicBinder.isFirst()){
                        musicBinder.playFromNet("http://yinyueshiting.baidu.com/data2/music/42783748/42783748.mp3?xcode=0de14d3e4689979bb4504f1dc3711450");
                        playIv.setImageResource(R.mipmap.stop);
                    }else {
                        musicBinder.continuePlay();
                        playIv.setImageResource(R.mipmap.stop);
                    }
                }

            }
        });

        //进度条设置
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isMoveBar = true ;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isMoveBar = false ;
                musicBinder.setSongPosition(seekBar.getProgress());

            }
        });

        // 注册广播
        MusicBroadCast musicBroadCast = new MusicBroadCast();
        IntentFilter intentFilter = new IntentFilter("musicBroad");
        registerReceiver(musicBroadCast,intentFilter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    //解绑服务的操作
    private void unbind(){
        unbindService(connection);
    }

    //广播接收器
    class MusicBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String str = intent.getStringExtra("progress");
            nowTv.setText(str);
        }
    }



}
